import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';

import { User } from './user';


@Injectable()
export class LoginDataProvider {
  private readonly baseUrl: string = 'https://api.bmob.cn/1';
  private readonly httpHeaders : HttpHeaders =new HttpHeaders({
    'X-Bmob-Application-Id':'0f23034457b7a694277ce9d417af0538',
    'X-Bmob-REST-API-Key':'743577773d54228e84ae14b11573af5a',
    'Content-Type':'application/json'
    });

  constructor(public http: HttpClient) {
   
  }
  /**
   * 登录数据处理
   * @param username 
   * @param password 
   */
  login(username:string,password : string): Observable<User>{
    let _parms = new HttpParams()
    .set('username',username)
    .set('password',password);
    return this.http.get<User>(`${this.baseUrl}/login`,{headers:this.httpHeaders,params: _parms});
  }
  /**
   * 注册数据处理
   * @param username 
   * @param password 
   */
  register(username:string,password:string): Observable<User>{
     let registerUser = {username:username,password:password};
     return this.http.post<User>(`${this.baseUrl}/users`,registerUser ,{headers: this.httpHeaders});
     }

}
